using WebRtcProject.Models;
using WebRtcProject.Models.Messages;

namespace WebRtcProject.Services
{
    /// <summary>
    /// WebRTC 专用 RabbitMQ 服务接口
    /// 针对嵌入式设备语音通话的信令传输优化
    /// </summary>
    public interface IWebRtcRabbitMQService : IDisposable
    {
        // ====================
        // 连接和初始化
        // ====================
        
        /// <summary>
        /// 初始化服务
        /// </summary>
        Task InitializeAsync();
        
        /// <summary>
        /// 检查连接状态
        /// </summary>
        Task<bool> IsConnectedAsync();
        
        /// <summary>
        /// 获取服务状态
        /// </summary>
        Task<object> GetServiceStatusAsync();

        // ====================
        // 嵌入式设备管理
        // ====================
        
        /// <summary>
        /// 注册嵌入式设备
        /// </summary>
        /// <param name="deviceId">设备ID</param>
        /// <param name="capabilities">设备能力</param>
        Task<bool> RegisterDeviceAsync(string deviceId, DeviceCapabilities capabilities);
        
        /// <summary>
        /// 注销嵌入式设备
        /// </summary>
        /// <param name="deviceId">设备ID</param>
        /// <param name="reason">注销原因</param>
        Task<bool> UnregisterDeviceAsync(string deviceId, string? reason = null);
        
        /// <summary>
        /// 检查设备是否在线
        /// </summary>
        /// <param name="deviceId">设备ID</param>
        Task<bool> IsDeviceOnlineAsync(string deviceId);
        
        /// <summary>
        /// 获取设备状态
        /// </summary>
        /// <param name="deviceId">设备ID</param>
        Task<DeviceStatusMessage?> GetDeviceStatusAsync(string deviceId);
        
        /// <summary>
        /// 获取所有在线设备列表
        /// </summary>
        Task<List<string>> GetOnlineDevicesAsync();
        
        /// <summary>
        /// 更新设备状态
        /// </summary>
        /// <param name="deviceId">设备ID</param>
        /// <param name="status">状态信息</param>
        Task UpdateDeviceStatusAsync(string deviceId, DeviceStatusMessage status);

        // ====================
        // WebRTC 信令发送
        // ====================
        
        /// <summary>
        /// 发送 WebRTC Offer
        /// </summary>
        /// <param name="fromDeviceId">发送设备ID</param>
        /// <param name="toDeviceId">接收设备ID</param>
        /// <param name="offer">Offer数据</param>
        /// <param name="sessionId">会话ID</param>
        /// <param name="roomId">房间ID</param>
        Task<bool> SendOfferAsync(string fromDeviceId, string toDeviceId, WebRtcOffer offer, 
            string? sessionId = null, string? roomId = null);
        
        /// <summary>
        /// 发送 WebRTC Answer
        /// </summary>
        /// <param name="fromDeviceId">发送设备ID</param>
        /// <param name="toDeviceId">接收设备ID</param>
        /// <param name="answer">Answer数据</param>
        /// <param name="sessionId">会话ID</param>
        /// <param name="roomId">房间ID</param>
        Task<bool> SendAnswerAsync(string fromDeviceId, string toDeviceId, WebRtcAnswer answer, 
            string sessionId, string? roomId = null);
        
        /// <summary>
        /// 发送 ICE Candidate
        /// </summary>
        /// <param name="fromDeviceId">发送设备ID</param>
        /// <param name="toDeviceId">接收设备ID</param>
        /// <param name="candidate">ICE候选数据</param>
        /// <param name="sessionId">会话ID</param>
        /// <param name="roomId">房间ID</param>
        Task<bool> SendIceCandidateAsync(string fromDeviceId, string toDeviceId, IceCandidate candidate, 
            string sessionId, string? roomId = null);
        
        /// <summary>
        /// 发送通话开始消息
        /// </summary>
        /// <param name="fromDeviceId">发送设备ID</param>
        /// <param name="toDeviceId">接收设备ID</param>
        /// <param name="sessionId">会话ID</param>
        /// <param name="roomId">房间ID</param>
        Task<bool> SendCallStartAsync(string fromDeviceId, string toDeviceId, string sessionId, 
            string? roomId = null);
        
        /// <summary>
        /// 发送通话结束消息
        /// </summary>
        /// <param name="fromDeviceId">发送设备ID</param>
        /// <param name="toDeviceId">接收设备ID</param>
        /// <param name="sessionId">会话ID</param>
        /// <param name="roomId">房间ID</param>
        /// <param name="reason">结束原因</param>
        Task<bool> SendCallEndAsync(string fromDeviceId, string toDeviceId, string sessionId, 
            string? roomId = null, string? reason = null);

        // ====================
        // 信令监听
        // ====================
        
        /// <summary>
        /// 开始监听设备信令
        /// </summary>
        /// <param name="deviceId">设备ID</param>
        /// <param name="handler">消息处理器</param>
        Task StartListeningAsync(string deviceId, IWebRtcSignalingHandler handler);
        
        /// <summary>
        /// 停止监听设备信令
        /// </summary>
        /// <param name="deviceId">设备ID</param>
        Task StopListeningAsync(string deviceId);
        
        /// <summary>
        /// 监听设备状态变化
        /// </summary>
        /// <param name="handler">状态处理器</param>
        Task StartStatusMonitoringAsync(IDeviceStatusHandler handler);
        
        /// <summary>
        /// 停止状态监听
        /// </summary>
        Task StopStatusMonitoringAsync();

        // ====================
        // 房间管理（可选）
        // ====================
        
        /// <summary>
        /// 广播消息到房间内所有设备
        /// </summary>
        /// <param name="roomId">房间ID</param>
        /// <param name="message">消息内容</param>
        /// <param name="excludeDeviceId">排除的设备ID</param>
        Task BroadcastToRoomAsync(string roomId, WebRtcSignalingMessage message, 
            string? excludeDeviceId = null);

        // ====================
        // 性能监控
        // ====================
        
        /// <summary>
        /// 获取延迟统计信息
        /// </summary>
        Task<LatencyStats> GetLatencyStatsAsync();
        
        /// <summary>
        /// 获取消息统计信息
        /// </summary>
        Task<MessageStats> GetMessageStatsAsync();
    }

    /// <summary>
    /// WebRTC 信令处理器接口
    /// </summary>
    public interface IWebRtcSignalingHandler
    {
        /// <summary>
        /// 处理接收到的信令消息
        /// </summary>
        /// <param name="message">信令消息</param>
        Task<bool> HandleSignalingMessageAsync(WebRtcSignalingMessage message);
        
        /// <summary>
        /// 处理消息发送失败
        /// </summary>
        /// <param name="message">失败的消息</param>
        /// <param name="error">错误信息</param>
        Task HandleMessageFailureAsync(WebRtcSignalingMessage message, Exception error);
    }

    /// <summary>
    /// 设备状态处理器接口
    /// </summary>
    public interface IDeviceStatusHandler
    {
        /// <summary>
        /// 处理设备上线
        /// </summary>
        /// <param name="deviceId">设备ID</param>
        /// <param name="capabilities">设备能力</param>
        Task HandleDeviceOnlineAsync(string deviceId, DeviceCapabilities capabilities);
        
        /// <summary>
        /// 处理设备离线
        /// </summary>
        /// <param name="deviceId">设备ID</param>
        /// <param name="reason">离线原因</param>
        Task HandleDeviceOfflineAsync(string deviceId, string? reason);
        
        /// <summary>
        /// 处理设备状态更新
        /// </summary>
        /// <param name="status">状态信息</param>
        Task HandleDeviceStatusUpdateAsync(DeviceStatusMessage status);
    }

    /// <summary>
    /// 延迟统计信息
    /// </summary>
    public class LatencyStats
    {
        public double AverageLatency { get; set; }
        public double MaxLatency { get; set; }
        public double MinLatency { get; set; }
        public int TotalMessages { get; set; }
        public int HighLatencyMessages { get; set; }
        public DateTime LastUpdated { get; set; }
    }

    /// <summary>
    /// 消息统计信息
    /// </summary>
    public class MessageStats
    {
        public long TotalSent { get; set; }
        public long TotalReceived { get; set; }
        public long FailedSent { get; set; }
        public long ExpiredMessages { get; set; }
        public Dictionary<string, long> MessageTypeStats { get; set; } = new();
        public Dictionary<string, long> DeviceStats { get; set; } = new();
        public DateTime LastUpdated { get; set; }
    }
} 